package com.ruoyi.wvp.task;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.wvp.sdk.monitor.IMonitorSdkService;
import com.ruoyi.wvp.sdk.monitor.entity.response.MonitorStatus;
import com.ruoyi.wvp.service.ISurveillanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author 蒋翔 SY100242
 * @date 2024/7/22 9:22
 * @email SY100242@hnlens.com
 */
@Component("syncDeviceStatusTask")
@Slf4j
public class SyncDeviceStatusTask {
    private static final int MAX = 300;

    public static final ExecutorService executorService = Executors.newSingleThreadExecutor();

    public void sync() {
        executorService.submit(this::updateSurveillanceStatus);
    }

    public  void updateSurveillanceStatus() {
        log.info("start sync device status task");
        IMonitorSdkService monitorSdkService = SpringUtils.getBean(IMonitorSdkService.class);
        ISurveillanceService surveillanceService = SpringUtils.getBean(ISurveillanceService.class);
        List<MonitorStatus> listMonitorStatus = monitorSdkService.getListMonitorStatus();
        List<String> onlineDevices = new ArrayList<>();
        List<String> offlineDevices = new ArrayList<>();
        listMonitorStatus.forEach(item -> {
            if ("1".equals(item.getWorkState())) {
                onlineDevices.add(item.getDeviceSerial());
            } else {
                offlineDevices.add(item.getDeviceSerial());
            }
        });
        try {
            DeviceOfflineMaxTask bean = SpringUtils.getBean(DeviceOfflineMaxTask.class);
            bean.notifyWarning(offlineDevices);
        } catch (BeansException e) {
            log.error("通知超过30失败");
        }
        if (offlineDevices.size() > MAX) {

            List<List<String>> partition = splitList(offlineDevices, 300);
            for (List<String> serialCodes : partition) {
                //更新离线
                surveillanceService.updateBySerialNumber(serialCodes, 0);
            }
        } else {
            surveillanceService.updateBySerialNumber(offlineDevices, 0);
        }

        if (onlineDevices.size() > MAX) {
            List<List<String>> onlinePartition = splitList(onlineDevices, 300);
            for (List<String> serialCodes : onlinePartition) {
                //更新在线
                surveillanceService.updateBySerialNumber(serialCodes, 1);
            }
        } else {
            surveillanceService.updateBySerialNumber(onlineDevices, 1);
        }


        log.info("end sync device status task");
    }

    private static <T> List<List<T>> splitList(List<T> list, int chunkSize) {
        return IntStream.range(0, (list.size() + chunkSize - 1) / chunkSize)
                .mapToObj(i -> list.subList(i * chunkSize, Math.min((i + 1) * chunkSize, list.size())))
                .collect(Collectors.toList());
    }


}
